﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Dropthings.Business.Facade;
using Dropthings.Business.Facade.Context;

namespace Dropthings.ReportViewData
{
    /// <summary>
    ///ReportViewDataProvide 的摘要说明
    /// </summary>
    public class ReportViewDataProvide
    {
        public ReportViewDataProvide()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        public List<ReportViewData> GetEnergyMonthDatas(string identifyCode, int year,int month)
        {
            List<ReportViewData> lists = new List<ReportViewData>();
            DataSet ds = null;
            using (Facade facade = new Facade(AppContext.GetContext(HttpContext.Current)))
            {
                ds = facade.ListAmmerterWaterGasByXun(year, month, identifyCode);
            }
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                int count = 0;
                string[] str = new string[3] { "A", "B", "C" };
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    lists.Add(ReportViewData.FetchData(dr,str[count]));
                    count++;
                }
            }
            return lists;
        }

        public ReportViewOrganizeBaseInfo GetReportViewOrganizeBaseInfo(string identifyCode)
        {
            ReportViewOrganizeBaseInfo obj = new ReportViewOrganizeBaseInfo();
            DataSet ds = null;
            using (Facade facade = new Facade(AppContext.GetContext(HttpContext.Current)))
            {
                ds = facade.GetOrganizeBaseInfo(identifyCode);
            }
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {

                obj = ReportViewOrganizeBaseInfo.FetchData(ds.Tables[0].Rows[0]);
            }
            return obj;
        }

        public List<AmmeterGasCoal> GetAmmeterGasCoal(string identifyCode, int year, int month)
        {
            List<AmmeterGasCoal> lists = new List<AmmeterGasCoal>();
            DataTable dt = null;
            using (Facade facade = new Facade(AppContext.GetContext(HttpContext.Current)))
            {
                dt = facade.GetThreeMonthWithAmmeterGas(year, month, identifyCode);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    lists.Add(AmmeterGasCoal.FetchData(dr));
                }
            }
            return lists;
        }

        public List<CurrentMonthMoney> GetCurrentMonthMoney(string identifyCode, int year, int month)
        {
            List<CurrentMonthMoney> lists = new List<CurrentMonthMoney>();
            DataSet ds = null;
            using (Facade facade = new Facade(AppContext.GetContext(HttpContext.Current)))
            {
                ds = facade.GetCurrentMonthMoney(year, month, identifyCode);
            }
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    lists.Add(CurrentMonthMoney.FetchData(dr));
                }
            }
            return lists;
        }
    }
    public class ReportViewData
    {
        public string RowNum { get; set; }
        public string ReadingMonth { get; set; }
        public double AmmeterMonthData { get; set; }
        public double AmmeterCoal { get; set; }
        public double AmmeterMoney { get; set; }
        public double WaterMonthData { get; set; }
        public double WaterMoney { get; set; }
        public double GasMonthData { get; set; }
        public double GasCoal { get; set; }
        public double GasMoney { get; set; }
        public double TotalData { get; set; }
        public double TotalMoney { get; set; }

        public static ReportViewData FetchData(DataRow dr,string num)
        {
            ReportViewData obj = new ReportViewData();
            obj.RowNum = num;
            obj.ReadingMonth = dr["ReadingMonth"].ToString();
            obj.AmmeterMonthData = Convert.ToDouble(dr["AmmeterMonthData"]);
            obj.AmmeterCoal = Convert.ToDouble(dr["AmmeterCoal"]);
            obj.AmmeterMoney = Convert.ToDouble(dr["AmmeterMoney"]);
            obj.WaterMonthData = Convert.ToDouble(dr["WaterMonthData"]);
            obj.WaterMoney = Convert.ToDouble(dr["WaterMoney"]);
            obj.GasMonthData = Convert.ToDouble(dr["GasMonthData"]);
            obj.GasCoal = Convert.ToDouble(dr["GasCoal"]);
            obj.GasMoney = Convert.ToDouble(dr["GasMoney"]);
            obj.TotalData = obj.AmmeterCoal + obj.GasCoal;
            obj.TotalMoney = obj.AmmeterMoney + obj.WaterMoney + obj.GasMoney;
            return obj;
        }
    }

    public class ReportViewOrganizeBaseInfo
    {
        public string OrganizeName { get; set; }
        public string OrganizeAddress { get; set; }
        public string PhoneNum { get; set; }
        public string OrganizeCode { get; set; }
        public string Postalcode { get; set; }
        public string EMailBox { get; set; }
        public string FaxNum { get; set; }

        public static ReportViewOrganizeBaseInfo FetchData(DataRow dr)
        {
            ReportViewOrganizeBaseInfo obj = new ReportViewOrganizeBaseInfo();
            obj.OrganizeName = dr["OrganizeName"].ToString();
            obj.OrganizeAddress = dr["OrganizeAddress"].ToString();
            obj.PhoneNum = dr["PhoneNum"].ToString();
            obj.OrganizeCode = dr["OrganizeCode"].ToString();
            obj.Postalcode = dr["Postalcode"].ToString();
            obj.EMailBox = dr["EMailBox"].ToString();
            obj.FaxNum = dr["FaxNum"].ToString();
            return obj;
        }
    }

    public class AmmeterGasCoal
    {
        public int ReadingMonth { get; set; }
        public double AmmeterCoal { get; set; }
        public double GasCoal { get; set; }

        public static AmmeterGasCoal FetchData(DataRow dr)
        {
            AmmeterGasCoal obj = new AmmeterGasCoal();
            obj.ReadingMonth = Convert.ToInt32(dr["ReadingMonth"]);
            obj.AmmeterCoal = Convert.ToDouble(dr["AmmeterCoal"]);
            obj.GasCoal = Convert.ToDouble(dr["GasCoal"]);
            return obj;
        }
    }

    public class CurrentMonthMoney
    {
        public string Category{get;set;}
        public double Money{get;set;}
        public static CurrentMonthMoney FetchData(DataRow dr)
        {
            CurrentMonthMoney obj=new CurrentMonthMoney();
            obj.Category=dr["Category"].ToString();
            obj.Money = Convert.ToDouble(dr["MonthMoney"]);
            return obj;
        }
    }
}